package com.kurt.controller;

import com.kurt.annotation.Log;
import com.kurt.common.PageResult;
import com.kurt.common.Result;
import com.kurt.dto.DriverDTO;
import com.kurt.dto.create.DriverCreateRequest;
import com.kurt.dto.query.DriverQueryRequest;
import com.kurt.dto.update.DriverUpdateRequest;
import com.kurt.service.DriverService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 驾驶员管理控制器
 * 提供驾驶员相关的REST API接口
 */
@Slf4j
@RestController
@RequestMapping("/drivers")
@RequiredArgsConstructor
public class DriverController extends BaseController {

    private final DriverService driverService;

    /**
     * 创建驾驶员
     *
     * @param createRequest 创建请求
     * @return 创建结果
     */
    @Log(title = "创建驾驶员", businessType = 1)
    @PostMapping
    public Result<DriverDTO> createDriver(@Valid @RequestBody DriverCreateRequest createRequest) {
        try {
            createRequest.setLoginUserId(getCurrentUserId());
            log.info("创建驾驶员请求: {}", createRequest.getName());
            DriverDTO driver = driverService.createDriver(createRequest);
            return Result.success("驾驶员创建成功", driver);
        } catch (Exception e) {
            log.error("创建驾驶员失败", e);
            return Result.error("创建驾驶员失败: " + e.getMessage());
        }
    }

    /**
     * 更新驾驶员
     *
     * @param updateRequest 更新请求
     * @return 更新结果
     */
    @Log(title = "更新驾驶员", businessType = 2)
    @PutMapping
    public Result<DriverDTO> updateDriver(@Valid @RequestBody DriverUpdateRequest updateRequest) {
        updateRequest.setLoginUserId(getCurrentUserId());
        try {
            log.info("更新驾驶员请求: {}", updateRequest.getId());
            DriverDTO driver = driverService.updateDriver(updateRequest);
            return Result.success("驾驶员更新成功", driver);
        } catch (Exception e) {
            log.error("更新驾驶员失败", e);
            return Result.error("更新驾驶员失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取驾驶员详情
     *
     * @param id 驾驶员ID
     * @return 驾驶员详情
     */
    @GetMapping("/{id}")
    public Result<DriverDTO> getDriverById(@PathVariable("id") Long id) {
        try {
            log.info("获取驾驶员详情请求: {}", id);
            DriverDTO driver = driverService.getDriverById(id);
            return Result.success("获取驾驶员详情成功", driver);
        } catch (Exception e) {
            log.error("获取驾驶员详情失败", e);
            return Result.error("获取驾驶员详情失败: " + e.getMessage());
        }
    }

    /**
     * 分页查询驾驶员
     *
     * @param queryRequest 查询请求
     * @return 分页结果
     */
    @PostMapping("/page")
    public Result<PageResult<DriverDTO>> getDriverPage(@RequestBody DriverQueryRequest queryRequest) {
        queryRequest.setLoginUserId(getCurrentUserId());
        try {
            log.info("分页查询驾驶员请求: {}", queryRequest);
            Page<DriverDTO> page = driverService.getDriverPage(queryRequest);
            PageResult<DriverDTO> pageResult = PageResult.of(
                    page.getNumber() + 1,
                    page.getSize(),
                    page.getTotalElements(),
                    page.getContent()
            );
            return Result.success("分页查询驾驶员成功", pageResult);
        } catch (Exception e) {
            log.error("分页查询驾驶员失败", e);
            return Result.error("分页查询驾驶员失败: " + e.getMessage());
        }
    }

    /**
     * 查询驾驶员列表
     *
     * @param queryRequest 查询请求
     * @return 驾驶员列表
     */
    @PostMapping("/list")
    public Result<List<DriverDTO>> getDriverList(@RequestBody DriverQueryRequest queryRequest) {
        queryRequest.setLoginUserId(getCurrentUserId());
        try {
            log.info("查询驾驶员列表请求: {}", queryRequest);
            List<DriverDTO> drivers = driverService.getDriverList(queryRequest);
            return Result.success("查询驾驶员列表成功", drivers);
        } catch (Exception e) {
            log.error("查询驾驶员列表失败", e);
            return Result.error("查询驾驶员列表失败: " + e.getMessage());
        }
    }

    /**
     * 删除驾驶员
     *
     * @param id 驾驶员ID
     * @return 删除结果
     */
    @Log(title = "删除驾驶员", businessType = 3)
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteDriver(@PathVariable("id") Long id) {
        try {
            log.info("删除驾驶员请求: {}", id);
            boolean success = driverService.deleteDriver(id);
            return Result.success("驾驶员删除成功", success);
        } catch (Exception e) {
            log.error("删除驾驶员失败", e);
            return Result.error("删除驾驶员失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除驾驶员
     *
     * @param ids 驾驶员ID列表
     * @return 删除结果
     */
    @Log(title = "批量删除驾驶员", businessType = 3)
    @DeleteMapping("/batch")
    public Result<Boolean> deleteDrivers(@RequestBody List<Long> ids) {
        try {
            log.info("批量删除驾驶员请求: {}", ids);
            boolean success = driverService.deleteDrivers(ids);
            return Result.success("批量删除驾驶员成功", success);
        } catch (Exception e) {
            log.error("批量删除驾驶员失败", e);
            return Result.error("批量删除驾驶员失败: " + e.getMessage());
        }
    }

    /**
     * 根据组织ID获取驾驶员列表
     *
     * @param organizationId 组织ID
     * @return 驾驶员列表
     */
    @GetMapping("/organization/{organizationId}")
    public Result<List<DriverDTO>> getDriversByOrganizationId(@PathVariable("organizationId") Long organizationId) {
        try {
            log.info("根据组织ID获取驾驶员列表请求: {}", organizationId);
            List<DriverDTO> drivers = driverService.getDriversByOrganizationId(organizationId);
            return Result.success("获取驾驶员列表成功", drivers);
        } catch (Exception e) {
            log.error("获取驾驶员列表失败", e);
            return Result.error("获取驾驶员列表失败: " + e.getMessage());
        }
    }

    /**
     * 检查身份证号是否存在
     *
     * @param idCard 身份证号
     * @return 是否存在
     */
    @GetMapping("/exists/id-card/{idCard}")
    public Result<Boolean> existsByIdCard(@PathVariable("idCard") String idCard) {
        try {
            log.info("检查身份证号是否存在请求: {}", idCard);
            boolean exists = driverService.existsByIdCard(idCard);
            return Result.success("检查身份证号是否存在成功", exists);
        } catch (Exception e) {
            log.error("检查身份证号是否存在失败", e);
            return Result.error("检查身份证号是否存在失败: " + e.getMessage());
        }
    }

    /**
     * 检查驾驶证号是否存在
     *
     * @param licenseNumber 驾驶证号
     * @return 是否存在
     */
    @GetMapping("/exists/license-number/{licenseNumber}")
    public Result<Boolean> existsByLicenseNumber(@PathVariable("licenseNumber") String licenseNumber) {
        try {
            log.info("检查驾驶证号是否存在请求: {}", licenseNumber);
            boolean exists = driverService.existsByDriverLicense(licenseNumber);
            return Result.success("检查驾驶证号是否存在成功", exists);
        } catch (Exception e) {
            log.error("检查驾驶证号是否存在失败", e);
            return Result.error("检查驾驶证号是否存在失败: " + e.getMessage());
        }
    }

    /**
     * 更新驾驶员状态
     *
     * @param id     驾驶员ID
     * @param status 状态
     * @return 更新结果
     */
    @Log(title = "更新驾驶员状态", businessType = 2)
    @PostMapping("/{id}/status")
    public Result<Boolean> updateStatus(@PathVariable("id") Long id, @RequestParam Integer status) {
        try {
            log.info("更新驾驶员状态请求: id={}, status={}", id, status);
            boolean success = driverService.updateStatus(id, status);
            return Result.success("驾驶员状态更新成功", success);
        } catch (Exception e) {
            log.error("更新驾驶员状态失败", e);
            return Result.error("更新驾驶员状态失败: " + e.getMessage());
        }
    }
}
